<?php

tsload(APPS_PATH.'/admin/Lib/Action/AdministratorAction.class.php');
/**
 * 后台部门管理.
 *
 * @author jason
 */
class PageAction extends AdministratorAction
{
    public $pageTitle = array(
                            'index' => '首页自由模块设置',
                            );

    public function _initialize()
    {
        $this->pageTitle['index'] = "首页自由模块设置";
        parent::_initialize();
    }

    public function index()
    {
        $this->pageTab[] = array('title' => '自由模块管理', 'tabHash' => 'index', 'url' => U('admin/page/index'));
        $this->pageTab[] = array('title' => '自由模块添加', 'tabHash' => 'addPage', 'url' => U('admin/page/addPage'));

        $listData = model('IndexPage')->getList();

        //显示分类HTML需要知道的对应字段
        //$this->assign('field', array('id' => 'department_id', 'name' => 'title', 'sort' => 'display_order'));
        $this->assign('_func', 'page_show');    //JS操作函数前缀
        $this->pageKeyList = array('id', 'show_number_info', 'picture', 'show_link', 'sort','is_audit', 'DOACTION');
        foreach ($listData['data'] as &$v) {
            if($v['show_number'] == 1){
                $v['show_number_info'] = '单行一个模块';
            }else if( $v['show_number'] == 2 ){
                $v['show_number_info'] = '单行二个模块';
            }else if( $v['show_number'] == 3 ){
                $v['show_number_info'] = '单行三个模块';
            }else {
                $v['show_number_info'] = '单行一个模块';
            }
            if( $v['show_number'] == 1 || $v['show_number'] == 2 || $v['show_number'] == 3){
                $arr1 = json_decode($v['picture'] , true);
                $v['picture'] = $arr1[0];
                $img = getImageUrlByAttachId($v['picture']);
                $v['picture'] = '<img src="'.$img.'" width="50px"  height="50px"/>';
                $arr2 = json_decode($v['show_link'] , true);
                $v['show_link'] = $arr2[0];
            }

            $v['is_audit'] = $v['is_audit'] && $v['is_audit'] == 1 ? '审核通过': '审核不通过' ; ;

            $v['DOACTION'] = '<a href="'.U('admin/page/editPage', array('tabHash' => 'editPage', 'id' => $v['id'])).'">编辑自由模块</a> ';

        }
        $this->notEmpty = array('title');
        $this->onsubmit = 'admin.checkDepartment(this)';

        $this->displayList($listData);
    }


    public function addPage(){
        $this->assign('pageTitle', '添加自由模块');
        $this->pageTab[] = array('title' => '自由模块管理', 'tabHash' => 'list',    'url' => U('admin/page/index'));
        $this->pageTab[] = array('title' => '添加自由模块', 'tabHash' => 'addPage', 'url' => U('admin/page/addPage'));
        $this->pageButton[] = array('title' => "添加一栏", 'onclick' => "admin.add_line()");
        $this->pageKeyList = array( 'show_number', array('picture', 'show_link') , array('picture', 'show_link') , array('picture', 'show_link') ,'sort','is_audit','state');
        $this->assign('otherIndexPage', 1); //自定义，下拉框另外两个模块
        $this->opt['show_number'] = array('1' => '单行一个模块', '2' => '单行二个模块', '3' => '单行三个模块' );
        $this->opt['is_audit'] = array('1' => '审核通过', '0' => '审核不通过');
        $this->opt['state'] = array('1' => '正常', '0' => '删除');
        $this->assign('add_edit','add') ;
        $this->notEmpty = array('news_title' );
        // 表单URL设置
        $this->savePostUrl = U('admin/page/doAddPage');
        //$this->onsubmit = 'admin.newsCheck(this)';
        $this->onload[] = "$('#search_ids').val('');";
        $this->displayIndexPage();
    }

    public function doAddPage()
    {
        $show_number = $_POST['show_number']; //1 => 只需要传一个图片
        $picture =  ($_POST['picture']);
        $p = 0;
        foreach($picture as $k1=>$p1){
            if(!empty($p1)){
                $p++;
            }
        }
        if(!in_array($show_number , [1,2,3])){
            $this->error( '请选择类型');
        }
        if($p < $show_number){
            $this->error( '请上传'.$show_number.'张图片！');
        }
        $return = array('status' => 1, 'data' => '保存成功');

        if (false) {
            $return['status'] = 0;
            $return['data'] = "信息未找到";
            $this->error($return['data']);
        }else{
            $picture_arr = [];$link_array = [];
            $show_number ;
            $j=0;
            foreach($_POST['picture'] as $picture){
                if($picture) {
                    //第一个元素允许添加多于1个，用途：做轮播图
                    if ($show_number > 1 && $j == $show_number) {
                        break;
                    }
                    $picture_arr[] = $picture;
                    $j++;
                }
            }
            $jj=0;
            foreach($_POST['show_link'] as $show_link){
                if( $show_link ){
                    //第一个元素允许添加多于1个，用途：做轮播图
                    if( $show_number > 1 && $jj == $show_number ){
                        break;
                    }
                    $link_array[] = $show_link;
                    $jj++;
                }
            }

            $save['show_number'] =  ($_POST['show_number']);
            $save['picture'] = json_encode($picture_arr);
            $save['show_link'] = json_encode($_POST['show_link']);
            $save['is_audit'] = intval($_POST['is_audit']) ; //审核
            $save['sort'] = intval($_POST['sort']) ;
            $save['state'] = 1;
            $save['add_time'] = time();
            $rs = model('IndexPage')->add($save);

            if ($rs) {
                $this->assign('jumpUrl', U('admin/page/index'));
                $this->success("保存成功");
            } else {
                $this->error("保存失败");
            }
        }

    }


    public function editPage(){
        $id = $_GET['id'];
        $this->assign('pageTitle', '编辑自由模块');
        $this->pageTab[] = array('title' => '自由模块管理', 'tabHash' => 'list',    'url' => U('admin/page/index'));
        $this->pageTab[] = array('title' => '添加自由模块', 'tabHash' => 'addPage', 'url' => U('admin/page/addPage'));
        $this->pageKeyList = array('id', 'show_number', array('picture', 'show_link') , array('picture', 'show_link') , array('picture', 'show_link') ,'sort','is_audit','state');
        $this->assign('otherIndexPage', 1); //自定义，下拉框另外两个模块
        $this->opt['show_number'] = array('1' => '单行一个模块', '2' => '单行二个模块', '3' => '单行三个模块' );
        $this->opt['is_audit'] = array('1' => '审核通过', '0' => '审核不通过');
        $this->opt['state'] = array('1' => '正常', '0' => '删除');
        $this->notEmpty = array('news_title' );
        $this->assign('add_edit','edit') ;
        // 表单URL设置
        $this->savePostUrl = U('admin/page/doEditPage');
        //$this->onsubmit = 'admin.newsCheck(this)';
        $this->onload[] = "$('#search_ids').val('');";
        $pageInfo = model('IndexPage')->where(['id'=> $id])->find();
        if($pageInfo){
            $pageInfo['picture']   = json_decode($pageInfo['picture']);
            $pageInfo['show_link'] = json_decode($pageInfo['show_link']);
            if($pageInfo['show_number'] == 2){
                $this->onload[] = '$(".choose_3").hide();';
            }else if($pageInfo['show_number'] == 1){
                if(count($pageInfo['picture']) == 2){
                    $this->onload[] = '$(".choose_2").show();';
                    $this->onload[] = '$(".choose_3").hide();';
                }else if(count($pageInfo['picture']) == 3){
                    $this->onload[] = '$(".choose_2").show();';
                    $this->onload[] = '$(".choose_3").show();';
                }else{
                    $this->onload[] = '$(".choose_3").hide();';
                    $this->onload[] = '$(".choose_2").hide();';
                }

            }
        }
        $this->displayIndexPage($pageInfo);
    }

    public function doEditPage(){
        $id = $_POST['id'];
        $pageInfo = model('IndexPage')->where(['id'=> $id])->find();
        if($id && $pageInfo){
            $pageInfo['picture']   = json_decode($pageInfo['picture']);
            $pageInfo['show_link'] = json_decode($pageInfo['show_link']);
        }else{
            $this->error("未找到该内容");
        }

        $show_number = $_POST['show_number']; //1 => 只需要传一个图片
        $picture =  ($_POST['picture']);
        $p = 0;
        foreach($picture as $k1=>$p1){
            if(!empty($p1)){
                $p++;
            }
        }
        if(!in_array($show_number , [1,2,3])){
            $this->error( '请选择类型');
        }
        if($p < $show_number){
            $this->error( '请上传'.$show_number.'张图片！');
        }
        $picture_arr = [];$link_array = [];
        $show_number ;
        $j=0;
        foreach($_POST['picture'] as $picture){
            if($picture) {
                if ($show_number > 1 && $j == $show_number) {
                    continue;
                }
                $picture_arr[] = $picture;
                $j++;
            }
        }
        $jj=0;
        foreach($_POST['show_link'] as $show_link){
            if( $show_link ){
                if($show_number > 1 &&  $jj == $show_number ){
                    continue;
                }
                $link_array[] = $show_link;
                $jj++;
            }
        }
        $save['show_number'] =  trim($_POST['show_number']);
        $save['picture']   = json_encode($picture_arr);
        $save['show_link'] = json_encode($link_array);
        $save['is_audit'] = intval($_POST['is_audit']) ; //审核
        $save['sort'] = intval($_POST['sort']) ;
        $save['state'] = intval($_POST['state']) ;
        $save['update_time'] = time();
        $save['is_audit'] = intval($_POST['is_audit']) ;
        $rs = model('IndexPage')->where( ['id'=> $id] )->save($save);

        if ($rs) {
            $this->assign('jumpUrl', U('admin/page/index'));
            $this->success("保存成功");
        } else {
            $this->error("保存失败");
        }
    }


    public function eight()
    {
        $this->eightlist();
    }

    public function eightlist()
    {
        $this->pageTab[] = array('title' => '首页八宫格模块管理', 'tabHash' => 'eight', 'url' => U('admin/page/eight'));
        $this->pageTab[] = array('title' => '八宫格模块添加', 'tabHash' => 'addEight', 'url' => U('admin/page/addEight'));

        $listData = model('IndexEight')->getList();

        //显示分类HTML需要知道的对应字段
        //$this->assign('field', array('id' => 'department_id', 'name' => 'title', 'sort' => 'display_order'));
        //$this->assign('_func', 'page_show');    //JS操作函数前缀
        $this->pageKeyList = array('id','title','picture_img', 'show_link', 'sort', 'is_audit', 'DOACTION');
        foreach ($listData['data'] as &$v) {
            if ($v['picture']) {
                $img = getImageUrlByAttachId($v['picture']);
            } else {
                $img = '';
            }
            $v['picture_img'] = '<img src="' . $img . '" width="50px"  height="50px"/>';
            $v['is_audit'] = $v['is_audit'] && $v['is_audit'] == 1 ? '审核通过' : '审核不通过';;
            $v['DOACTION'] = '<a href="' . U('admin/page/editEight', array('tabHash' => 'editPage', 'id' => $v['id'])) . '">编辑首页八格模块</a> ';

        }

        $this->displayList($listData);
    }

    public function addEight()
    {
        $this->assign('pageTitle', '添加八宫格');
        $this->pageTab[] = array('title' => '首页八宫格模块管理', 'tabHash' => 'eight', 'url' => U('admin/page/eight'));
        $this->pageTab[] = array('title' => '添加八宫格', 'tabHash' => 'addEight', 'url' => U('admin/page/addEight'));
        $this->pageKeyList = array('title', 'picture', 'show_link', 'sort', 'is_audit', 'state');

        $this->opt['is_audit'] = array('1' => '审核通过', '0' => '审核不通过');
        $this->opt['state'] = array('1' => '正常', '0' => '删除');
        $this->notEmpty = array('title');
        $this->notEmpty = array('picture');
        $this->notEmpty = array('show_link');
        // 表单URL设置
        $this->savePostUrl = U('admin/page/doAddEight');
        //$this->onsubmit = 'admin.newsCheck(this)';
        $this->onload[] = "$('#search_ids').val('');";
        $this->displayConfig();
    }

    public function doAddEight()
    {
        $title = ($_POST['title']);
        $picture = ($_POST['picture']);
        $show_link = ($_POST['show_link']);

        if (empty($title)) {
            $this->error('请输入标题');
        }
        if (empty($picture)) {
            $this->error('请上传图片');
        }
        if (empty($show_link)) {
            $this->error('请填写链接');
        }

        if (false) {
            $return['status'] = 0;
            $return['data'] = "信息未找到";
            $this->error($return['data']);
        } else {
            $save['title'] = $title;
            $save['picture'] = trim($picture);
            $save['show_link'] = $show_link;
            $save['is_audit'] = intval($_POST['is_audit']); //审核
            $save['sort'] = intval($_POST['sort']);
            $save['state'] = 1;
            $save['add_time'] = time();
            $rs = model('IndexEight')->add($save);

            if ($rs) {
                $this->assign('jumpUrl', U('admin/page/eight'));
                $this->success("保存成功");
            } else {
                $this->error("保存失败");
            }
        }

    }

    public function editEight()
    {
        $id = $_GET['id'];
        if (empty($id)) {
            $this->error('参数不正确！！');
        }
        $this->assign('pageTitle', '编辑八宫格模块');
        $this->pageTab[] = array('title' => '八宫格管理', 'tabHash' => 'eight', 'url' => U('admin/page/eight'));
        $this->pageTab[] = array('title' => '编辑八宫格', 'tabHash' => 'editEight', 'url' => U('admin/page/editEight'));
        $this->pageKeyList = array('id','title', 'picture', 'show_link', 'sort', 'is_audit', 'state');

        $this->opt['is_audit'] = array('1' => '审核通过', '0' => '审核不通过');
        $this->opt['state'] = array('1' => '正常', '0' => '删除');
        $this->notEmpty = array('title');
        $this->notEmpty = array('picture');
        $this->notEmpty = array('show_link');
        // 表单URL设置
        $this->savePostUrl = U('admin/page/doEditEight');

        $this->onload[] = "$('#search_ids').val('');";
        $pageInfo = model('IndexEight')->where(['id' => $id])->find();
        if ($pageInfo) {
            $pageInfo['title'] = ($pageInfo['title']);
            $pageInfo['picture'] = ($pageInfo['picture']);
            $pageInfo['show_link'] = ($pageInfo['show_link']);
        }
        $this->displayConfig($pageInfo);
    }

    public function doEditEight()
    {
        $id = intval($_POST['id']);
        $title = $_POST['title'];
        $picture = $_POST['picture'];
        $show_link = $_POST['show_link'];
        if (!$id) {
            $this->error("未找到该id");
        } else {
            $pageInfo = model('IndexEight')->where(['id' => $id])->find();
            if (!$pageInfo) {
                $this->error("未找到该内容");
            }
        }
        $save['title'] = $title;
        $save['picture'] = $picture;
        $save['show_link'] = $show_link;
        $save['is_audit'] = intval($_POST['is_audit']); //审核
        $save['sort'] = intval($_POST['sort']);
        $save['state'] = intval($_POST['state']);
        $save['update_time'] = time();
        $save['is_audit'] = intval($_POST['is_audit']);
        $rs = model('IndexEight')->where(['id' => $id])->save($save);

        if ($rs) {
            $this->assign('jumpUrl', U('admin/page/eight'));
            $this->success("保存成功");
        } else {
            $this->error("保存失败");
        }
    }
}
